package com.yanfan.permission.service.impl;

import java.util.List;

import com.yanfan.common.utils.AESUtil;
import com.yanfan.common.utils.Md5Utils;
import com.yanfan.permission.domain.ApiCredentials;
import com.yanfan.permission.mapper.ApiCredentialsMapper;
import com.yanfan.permission.service.IApiCredentialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 存储API认证信息的，用于管理不同API的认证信息Service业务层处理
 *
 * @author kerwincui
 * @date 2024-08-12
 */
@Service
public class ApiCredentialsServiceImpl implements IApiCredentialsService
{
    @Autowired
    private ApiCredentialsMapper apiCredentialsMapper;

    /**
     * 查询存储API认证信息的，用于管理不同API的认证信息
     *
     * @param id 存储API认证信息的，用于管理不同API的认证信息主键
     * @return 存储API认证信息的，用于管理不同API的认证信息
     */
    @Override
    public ApiCredentials selectApiCredentialsById(Long id)
    {
        ApiCredentials credentials = apiCredentialsMapper.selectApiCredentialsById(id);
        if(credentials.getAuthType().equals("username_password")){
            credentials.setAuthType("账号密码");
            credentials.setPassword(AESUtil.decryptAES(credentials.getPassword()));
        }else if(credentials.getAuthType().equals("app_id_secret")){
            credentials.setAuthType("应用ID+密钥");
            credentials.setAppSecret(AESUtil.decryptAES(credentials.getAppSecret()));
        }else {
            credentials.setAuthType("令牌");
        }

        return credentials;
    }

    /**
     * 查询存储API认证信息的，用于管理不同API的认证信息列表
     *
     * @param apiCredentials 存储API认证信息的，用于管理不同API的认证信息
     * @return 存储API认证信息的，用于管理不同API的认证信息
     */
    @Override
    public List<ApiCredentials> selectApiCredentialsList(ApiCredentials apiCredentials)
    {
        return apiCredentialsMapper.selectApiCredentialsList(apiCredentials);
    }

    /**
     * 新增存储API认证信息的，用于管理不同API的认证信息
     *
     * @param apiCredentials 存储API认证信息的，用于管理不同API的认证信息
     * @return 结果
     */
    @Override
    public int insertApiCredentials(ApiCredentials apiCredentials)
    {
        if(apiCredentials.getAuthType().equals("1") && apiCredentials.getEncryptType() == 1){

            apiCredentials.setPassword(Md5Utils.md5(apiCredentials.getPassword()));
            return apiCredentialsMapper.insertApiCredentials(apiCredentials);
        }else if(apiCredentials.getAuthType().equals("1") && apiCredentials.getEncryptType() == 2){
                apiCredentials.setPassword(AESUtil.encryptAES(apiCredentials.getPassword()));
                return apiCredentialsMapper.insertApiCredentials(apiCredentials);
        }
        else if(apiCredentials.getAuthType().equals("2") && apiCredentials.getEncryptType() == 1){
            apiCredentials.setAppSecret(Md5Utils.md5(apiCredentials.getAppSecret()));
            return apiCredentialsMapper.insertApiCredentials(apiCredentials);
        }else if(apiCredentials.getAuthType().equals("2") && apiCredentials.getEncryptType() == 2){
            apiCredentials.setAppSecret(AESUtil.encryptAES(apiCredentials.getAppSecret()));
            return apiCredentialsMapper.insertApiCredentials(apiCredentials);
        }else {
            return apiCredentialsMapper.insertApiCredentials(apiCredentials);
        }
    }

    /**
     * 修改存储API认证信息的，用于管理不同API的认证信息
     *
     * @param apiCredentials 存储API认证信息的，用于管理不同API的认证信息
     * @return 结果
     */
    @Override
    public int updateApiCredentials(ApiCredentials apiCredentials)
    {
        return apiCredentialsMapper.updateApiCredentials(apiCredentials);
    }

    /**
     * 批量删除存储API认证信息的，用于管理不同API的认证信息
     *
     * @param ids 需要删除的存储API认证信息的，用于管理不同API的认证信息主键
     * @return 结果
     */
    @Override
    public int deleteApiCredentialsByIds(Long[] ids)
    {
        return apiCredentialsMapper.deleteApiCredentialsByIds(ids);
    }

    /**
     * 删除存储API认证信息的，用于管理不同API的认证信息信息
     *
     * @param id 存储API认证信息的，用于管理不同API的认证信息主键
     * @return 结果
     */
    @Override
    public int deleteApiCredentialsById(Long id)
    {
        return apiCredentialsMapper.deleteApiCredentialsById(id);
    }
}
